package com.hc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.dao.UserMapper;
import com.hc.pojo.TbUser;
import com.hc.service.TbUserService;
import com.hc.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

/*****
 *
 */
public class TbUserServiceImpl implements TbUserService {
    private SqlSession session= MybatisUtils.getSqlSession();
    private UserMapper um=session.getMapper(UserMapper.class);

    /****
     *
     * @param pageNum 页码
     * @param pageSize 每页显示的条数
     * @return page信息
     */
    @Override
    public PageInfo listUserByPage(int pageNum, int pageSize) {

        PageHelper.startPage(pageNum,pageSize);

        List<TbUser> userList=um.findAll();
       /*if(pageNum == (-1)){
            int i=userList.size()%pageSize;
            if(i==0){
                pageNum=userList.size()/pageSize;
            }else {
                pageNum=userList.size()/pageSize+1;
            }
        }*/
        PageInfo<TbUser> info=new PageInfo(userList);
        return info;
    }

    @Override
    public TbUser selectEmailAndPassWord(String email) {
        TbUser user=um.selectByEmail(email);
        return user;
    }

    @Override
    public List<TbUser> findAll() {
        return um.findAll();
    }

    @Override
    public boolean addUser(TbUser tbUser) {
        boolean b=um.addTbUser(tbUser);
        session.commit();
        return b;
    }

    @Override
    public int deleteById(Long id) {
        int line=um.deleteById(id);
        session.commit();
        return line;
    }

    @Override
    public int updateTbUer(TbUser tbUser) {
        int line=um.updateTbUer(tbUser);
        session.commit();
        return line;
    }

    @Override
    public TbUser findById(Long id) {
        return um.findByid(id);
    }

    @Override
    public int batchDelete(String[] ids) {
        int lines=um.batchDelete(ids);
        session.commit();
        return lines;
    }

    @Override
    public List<TbUser> likeSearch(Map map) {
        return um.findLike(map);
    }
}
